﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.Office.Word.Server.Conversions;
using System.Collections;
using System.IO;
using System.Threading;

namespace SPWorks
{
    class ConvertDocumentsClass
    {
        public string sourceLibrary;
        public string destinationLibrary;
        public string extension;
        ConversionJobStatus status;
        public string[] files;
        public enum DocSaveFormat
        {
            Document, Document97, DocumentMacroEnabled, MHTML, PDF, RTF, Template, Template97, TemplateMacroEnabled, XML, XPS
        };

        public enum DocSaveBehaviour
        {
            AlwaysOverwrite, AppendIfPossible, AppendOnly, NeverOverwrite
        };

        public Guid ConvertDocuments(SPSite spSite, SPWeb spWeb, ArrayList files, DocSaveFormat docSaveFormat, DocSaveBehaviour docSaveBehaviour)
        {
            SaveFormat savFormat = getSaveFormat(docSaveFormat);
            SaveBehavior savBehaviour = getSaveBehavior(docSaveBehaviour);
            string wordAutomationServiceName = "Word Automation Services";
            ConversionJob job = new ConversionJob(wordAutomationServiceName);
            job.UserToken = spSite.UserToken;
            job.Settings.UpdateFields = true;
            job.Settings.OutputFormat = savFormat;
            job.Settings.OutputSaveBehavior = savBehaviour;
            job.Settings.AddThumbnail = true;            
            foreach (string file in files)
            {
                job.AddFile(spWeb.Url + "/" + sourceLibrary + "/" + file, spWeb.Url + "/" + destinationLibrary + "/" + Path.GetFileNameWithoutExtension(file) + extension);
            }            
            job.Start();
            status = new ConversionJobStatus(wordAutomationServiceName, job.JobId, null);
            return job.JobId;
        }
        public string getResult(Guid JobId)
        {
            status = new ConversionJobStatus("Word Automation Services", JobId, null);
            string result = string.Empty;
            if (status.Count == status.Succeeded + status.Failed)
            {
                result = "Completed, Successful: " + status.Succeeded + ", Failed: " + status.Failed;
            }
            else
            {
                result = "In progress, Successful: " + status.Succeeded + ", Failed: " + status.Failed;
            }
            return result;
        }

        private SaveBehavior getSaveBehavior(DocSaveBehaviour docSaveBehaviour)
        {
            SaveBehavior savBehavior = SaveBehavior.NeverOverwrite;
            switch (docSaveBehaviour)
            {
                case DocSaveBehaviour.AlwaysOverwrite:
                    savBehavior = SaveBehavior.AlwaysOverwrite;
                    break;
                case DocSaveBehaviour.AppendIfPossible:
                    savBehavior = SaveBehavior.AppendIfPossible;
                    break;
                case DocSaveBehaviour.AppendOnly:
                    savBehavior = SaveBehavior.AppendOnly;
                    break;
                case DocSaveBehaviour.NeverOverwrite:
                    savBehavior = SaveBehavior.NeverOverwrite;
                    break;
            }
            return savBehavior;
        }

        private SaveFormat getSaveFormat(DocSaveFormat doc)
        {
            SaveFormat savFormat = SaveFormat.Automatic;
            switch (doc)
            {
                case DocSaveFormat.Document:
                    savFormat = SaveFormat.Document;
                    extension = ".docx";
                    break;
                case DocSaveFormat.Document97:
                    savFormat = SaveFormat.Document97;
                    extension = ".doc";
                    break;
                case DocSaveFormat.DocumentMacroEnabled:
                    savFormat = SaveFormat.DocumentMacroEnabled;
                    extension = ".docx";
                    break;
                case DocSaveFormat.MHTML:
                    savFormat = SaveFormat.MHTML;
                    extension = ".mht";
                    break;
                case DocSaveFormat.PDF:
                    savFormat = SaveFormat.PDF;
                    extension = ".pdf";
                    break;
                case DocSaveFormat.RTF:
                    savFormat = SaveFormat.RTF;
                    extension = ".rtf";
                    break;
                case DocSaveFormat.Template:
                    savFormat = SaveFormat.Template;
                    extension = ".dotx";
                    break;
                case DocSaveFormat.Template97:
                    savFormat = SaveFormat.Template97;
                    extension = ".dot";
                    break;
                case DocSaveFormat.TemplateMacroEnabled:
                    savFormat = SaveFormat.TemplateMacroEnabled;
                    extension = ".dotm";
                    break;
                case DocSaveFormat.XML:
                    savFormat = SaveFormat.XML;
                    extension = ".xml";
                    break;
                case DocSaveFormat.XPS:
                    savFormat = SaveFormat.XPS;
                    extension = ".xps";
                    break;
            }
            return savFormat;
        }

        
    }
}
